package com.android.powerbankpad.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.powerbankpad.R;
import com.android.powerbankpad.bean.OrderBean;
import com.ws.universal.tools.utils.ScreenUtils;
import com.ws.universal.tools.utils.StringUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class PosRentResultDialog extends Dialog {
    private Context mContext;
    private TextView tvOrderNumber;
    private TextView tvRentalTime;
    private TextView tvReturnTime;
    private TextView tvUsageTime;
    private TextView tvTotalAmount;
    private int layoutId;

    public PosRentResultDialog(@NonNull Context context) {
        super(context, R.style.dialog_alarm);
        mContext = context;
        initUI();
    }

    public void setData(OrderBean orderBean) {

        tvOrderNumber.setText(orderBean.order_no);
        tvRentalTime.setText(orderBean.create_time);
        tvReturnTime.setText(orderBean.end_time);
        tvUsageTime.setText(orderBean.use_time);
        tvTotalAmount.setText(StringUtils.getString(orderBean.currency, orderBean.amount));

    }

    private void initUI() {

        if (ScreenUtils.getScreenWidth() < ScreenUtils.getScreenHeight()) {
            layoutId = R.layout.dialog_alarm_pos_portrait;
        } else {
            layoutId = R.layout.dialog_alarm_pos_landscape;
        }
        View dlgView = LayoutInflater.from(mContext).inflate(layoutId, null);
        tvOrderNumber = dlgView.findViewById(R.id.tvOrderNumber);
        tvRentalTime = dlgView.findViewById(R.id.tvRentalTime);
        tvReturnTime = dlgView.findViewById(R.id.tvReturnTime);
        tvUsageTime = dlgView.findViewById(R.id.tvUsageTime);
        tvTotalAmount = dlgView.findViewById(R.id.tvTotalAmount);

        //setContentView(dlgView);


//
        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setGravity(Gravity.CENTER);
        window.setContentView(dlgView);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.FILL_PARENT;
        window.setAttributes(lp);

        setCanceledOnTouch(false);

    }

    @Override
    public void show() {
        super.show();
        //自动关闭
        Observable.timer(10000L, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Long aLong) {

                    }

                    @Override
                    public void onError(Throwable e) {
                        cancel();
                    }

                    @Override
                    public void onComplete() {
                        dismiss();
                    }
                });
    }

    public void setCanceledOnTouch(boolean isCanceledOnTouch) {
        setCanceledOnTouchOutside(isCanceledOnTouch);
    }
}
